﻿using System;
using System.Reflection;
using System.Windows.Forms;

namespace QL_Truong_Hoc.HocTap
{
    public partial class frmHocSinhLopVanHoa : DevExpress.XtraEditors.XtraForm
    {
        private const string mcstrRoleCode = "lop_hoc";

        private int mintLopID = clsConst.gcintNoneValue;
        private int mintTrinhDo = clsConst.gcintNoneValue;
        private int mintNghe = clsConst.gcintNoneValue;
        private int mintHsID = clsConst.gcintNoneValue;
        private int mintKVH = clsConst.gcintNoneValue;

        public frmHocSinhLopVanHoa()
        {
            InitializeComponent();
        }

        private void frmHocSinhLop_Load(object sender, EventArgs e)
        {
            if (!clsCommon.fncCheckRole(Program.glstRoles, mcstrRoleCode))
            {
                this.BeginInvoke(new MethodInvoker(this.Close));
                return;
            }

            // TODO: This line of code loads data into the 'qLTHDataSet.mTrinhDoDaoTao' table. You can move, or remove it, as needed.
            this.mTrinhDoDaoTaoTableAdapter.Fill(this.qLTHDataSet.mTrinhDoDaoTao);
            // TODO: This line of code loads data into the 'qLTHDataSet.mLopVanHoaSelectAllWithDetail' table. You can move, or remove it, as needed.
            this.mLopVanHoaSelectAllWithDetailTableAdapter.Fill(this.qLTHDataSet.mLopVanHoaSelectAllWithDetail);
            // TODO: This line of code loads data into the 'qLTHDataSet.mChucVuLop' table. You can move, or remove it, as needed.
            this.mChucVuLopTableAdapter.Fill(this.qLTHDataSet.mChucVuLop);
            // TODO: This line of code loads data into the 'qLTHDataSet.mGioiTinh' table. You can move, or remove it, as needed.
            this.mGioiTinhTableAdapter.Fill(this.qLTHDataSet.mGioiTinh);
            // TODO: This line of code loads data into the 'qLTHDataSet.mKhoaHoc' table. You can move, or remove it, as needed.
            this.mKhoaHocTableAdapter.Fill(this.qLTHDataSet.mKhoaHoc);
        }

        private void grdControlDsLop_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            try
            {
                //chỉ kích hoạt với chuột trái
                if (e.Button != System.Windows.Forms.MouseButtons.Left) return;

                mintLopID = clsCommon.fncCnvNullToInt(dgvDsLop.GetFocusedRowCellValue(colLVH_ID));

                if (mintLopID <= 0)
                { return; }

                //lấy các thông tin liên quan
                mintKVH = clsCommon.fncCnvNullToInt(dgvDsLop.GetFocusedRowCellValue(colKVH_ID));
                mintTrinhDo = clsCommon.fncCnvNullToInt(dgvDsLop.GetFocusedRowCellValue(colTD_ID));

                //fill thông tin lớp
                DTO.MLopVanHoaDTO objLop = DAO.MLopVanHoaDAO.Select(Program.gobjDb, mintLopID);
                txtTenLop.Text = objLop.LVH_ten;
                txtQDso.Text = objLop.LVH_quyetdinh;
                txtDiaDiem.Text = objLop.LVH_diadiem;
                txtChungChi.Text = objLop.LVH_chungchi;

                dtpBatDau.ResetText();
                if (objLop.LVH_batdau > DateTime.MinValue) dtpBatDau.EditValue = objLop.LVH_batdau;

                dtpKetThuc.ResetText();
                if (objLop.LVH_ketthuc > DateTime.MinValue) dtpKetThuc.EditValue = objLop.LVH_ketthuc;

                dtpQDngay.ResetText();
                if (objLop.LVH_quyetdinh_ngay > DateTime.MinValue) dtpQDngay.EditValue = objLop.LVH_quyetdinh_ngay;

                //fill danh sách học sinh
                this.tLopVanHoaHocSinhWithDetailTableAdapter.GetData(mintLopID);
                this.tLopVanHoaHocSinhWithDetailTableAdapter.Fill(qLTHDataSet.tLopVanHoaHocSinhWithDetail, mintLopID);
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void mniAdd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                if (mintLopID <= clsConst.gcintNoneValue)
                {
                    clsCommon.fncMsgInfo("Hãy chọn một lớp học.");
                    dgvDsLop.Focus();
                    return;
                }

                using (frmChonHocSinhDuTuyen frmChon = new frmChonHocSinhDuTuyen(frmChonHocSinhDuTuyen.FORM_MODE.HOC_VAN_HOA, mintTrinhDo, mintNghe))
                {
                    //add sự kiện handler để bắt việc 1 học sinh được chọn
                    frmChon.evnHocSinhSelected += new frmChonHocSinhDuTuyen.HocSinhSelected(xAddNewHandler);
                    frmChon.ShowDialog();
                }
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void xAddNewHandler(int intHsID)
        {
            try
            {
                //add học sinh vào lớp
                if (!xAddNew(intHsID, mintLopID)) return;

                //thêm 1 dòng mới trong grid -> sinh sự kiện dgvDsHocSinh_InitNewRow để add học sinh detail
                mintHsID = intHsID;
                dgvDsHocSinh.AddNewRow();
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private bool xAddNew(int intHsID, int intLopID)
        {
            bool blnResult = false;
            try
            {
                bool blnSuccess = true;

                Program.gobjTrans = Program.gobjConn.BeginTransaction();

                //học sinh lớp
                DTO.TLopVanHoaHocSinhDTO objLop_Hocsinh = new DTO.TLopVanHoaHocSinhDTO();
                objLop_Hocsinh.HS_ID = intHsID;
                objLop_Hocsinh.LVH_ID = intLopID;

                try
                {
                    blnSuccess = blnSuccess && DAO.TLopVanHoaHocSinhDAO.Insert(Program.gobjDb, Program.gobjTrans, objLop_Hocsinh);

                    if (blnSuccess) { Program.gobjTrans.Commit(); return true; }
                    else { Program.gobjTrans.Rollback(); }
                }
                catch { Program.gobjTrans.Rollback(); }
            }
            catch (Exception ex)
            {
                clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message);
                clsCommon.fncMsgError("Lưu dữ liệu không thành công. Hãy kiểm tra dữ liệu và thử lại.");
            }
            return blnResult;
        }

        private void dgvDsHocSinh_InitNewRow(object sender, DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs e)
        {
            try
            {
                //lấy chi tiết;
                DTO.THocSinhDTO objHS = DAO.THocSinhDAO.Select(Program.gobjDb, mintHsID);

                dgvDsHocSinh.SetRowCellValue(e.RowHandle, colHS_ID, objHS.HS_ID);
                dgvDsHocSinh.SetRowCellValue(e.RowHandle, colLVH_ID1, mintLopID);
                dgvDsHocSinh.SetRowCellValue(e.RowHandle, colhs_hodem, objHS.Hs_hodem);
                dgvDsHocSinh.SetRowCellValue(e.RowHandle, colhs_ten, objHS.Hs_ten);
                if (objHS.Hs_ngaysinh > DateTime.MinValue) dgvDsHocSinh.SetRowCellValue(e.RowHandle, colhs_ngaysinh, objHS.Hs_ngaysinh);

                dgvDsHocSinh.SetRowCellValue(e.RowHandle, colhs_noisinh, objHS.Hs_noisinh);
                dgvDsHocSinh.SetRowCellValue(e.RowHandle, colhs_gioitinh, objHS.Hs_gioitinh);
                dgvDsHocSinh.SetRowCellValue(e.RowHandle, colhs_hokhau, objHS.Hs_hokhau);
                dgvDsHocSinh.SetRowCellValue(e.RowHandle, colhs_uutien, objHS.Hs_uutien);
                dgvDsHocSinh.SetRowCellValue(e.RowHandle, colhs_nguyenvongsaukhihoc, objHS.Hs_nguyenvongsaukhihoc);
                dgvDsHocSinh.SetRowCellValue(e.RowHandle, colCVL_ID, DBNull.Value);

                dgvDsHocSinh.UpdateCurrentRow();
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void gridControlDsHocSinh_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            try
            {
                int intHsID;

                intHsID = clsCommon.fncCnvNullToInt(dgvDsHocSinh.GetFocusedRowCellValue(colHS_ID));

                if (intHsID <= 0) return;

                Cursor.Current = Cursors.WaitCursor;
                frmHocSinhChiTiet frmHocSinh = new frmHocSinhChiTiet(intHsID);
                frmHocSinh.MdiParent = this.ParentForm;
                frmHocSinh.Show();
                Cursor.Current = Cursors.Default;
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void mniDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                int intHsID;

                intHsID = clsCommon.fncCnvNullToInt(dgvDsHocSinh.GetFocusedRowCellValue(colHS_ID));

                if (intHsID <= 0) return;

                if (!clsCommon.fncMsgConfirm("Bạn có chắc muốn xóa học sinh này khỏi danh sách?\nToàn bộ điểm của học sinh cũng sẽ bị xóa, nếu muốn giữ lại điểm thì sử dụng chức năng \"Chuyển lớp nghề\"")) return;

                bool blnSuccess = true;

                Program.gobjTrans = Program.gobjConn.BeginTransaction();

                try
                {
                    blnSuccess = blnSuccess && DAO.TLopVanHoaHocSinhDAO.Delete(Program.gobjDb, Program.gobjTrans, mintLopID, intHsID);
                    blnSuccess = blnSuccess && DAO.THocSinhMonVanHoaDAO.Delete(Program.gobjDb, Program.gobjTrans, string.Format("HS_ID = {0}", intHsID));

                    if (blnSuccess)
                    {
                        Program.gobjTrans.Commit();
                        dgvDsHocSinh.DeleteRow(dgvDsHocSinh.FocusedRowHandle);
                    }
                    else
                    {
                        Program.gobjTrans.Rollback();
                        clsCommon.fncMsgError("Xóa học sinh thất bại. Xin hãy thử lại.");
                    }
                }
                catch
                {
                    Program.gobjTrans.Rollback();
                    clsCommon.fncMsgError("Xóa học sinh thất bại. Xin hãy thử lại.");
                }
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }
    }
}